#include <cstdio>
int main()
{
    int N, M, L;
    scanf("%d", &N);
    scanf("%d", &M);
    int i, j;
    int order[201];
    int seq[10001];
    for (i = 0; i < M; ++i)
    {
        scanf("%d", order + i);
    }
    scanf("%d", &L);
    for (i = 0; i < L; ++i)
    {
        scanf("%d", seq + i);
    }
    //boundary
    int dp[201][10001] = {0};
    if (order[0] == seq[0])
        dp[0][0] = 1;
    for (j = 1; j < L; ++j)
    {
        if (order[0] == seq[j])
        {
            dp[0][j] = dp[0][j - 1] + 1;
        }
        else
        {
            dp[0][j] = dp[0][j - 1];
        }
    }
    //State transition equation
    for (i = 1; i < M; ++i)
    {
        for (j = 1; j < L; ++j)
        {
            if (order[i] == seq[j])
            {
                dp[i][j] = (dp[i][j - 1] > dp[i - 1][j - 1] ? dp[i][j - 1] : dp[i - 1][j]) + 1;
            }
            else
            {
                dp[i][j] = dp[i][j - 1] > dp[i - 1][j - 1] ? dp[i][j - 1] : dp[i - 1][j];
            }
        }
    }
    printf("%d\n", dp[M - 1][L - 1]);
}